package com.et.api

import android.annotation.SuppressLint
import com.et.drive.info.LightStatusEnum
import com.et.drive.info.LightTemperatureInfo
import com.et.drive.info.TemperatureStateEnum
import com.et.drive.run.Run
import com.et.protocol.ano.Guide2Code
import com.et.util.insertByte
import com.et.util.toBCDByte
import com.et.util.toByteArray
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*

object MachineConfig {

    fun setChannelInfo(
        deviceNumber: Int,
        channel: Int,
        goodsID: Int,
        priceCent: Int,
        gaming: Boolean
    ): Boolean {
        val guide = Run.getGuide() ?: return false
        val priceDime = (priceCent.toBigDecimal().divide(BigDecimal(10))).toInt().toByteArray(2)
        val data = byteArrayOf(
            0, 0,
            deviceNumber.toByte(), channel.toByte(), goodsID.toByte(),
            priceDime[0], priceDime[1],
            if (gaming) 1 else 0,
            0, 0
        )
        return guide.guide2(Guide2Code.GUI_04, data)
    }

    fun setGoodsNumber(
        deviceNumber: Int,
        channel: Int,
        number: Int,
    ): Boolean {
        val guide = Run.getGuide() ?: return false
        val data = byteArrayOf(
            0, 0,
            deviceNumber.toByte(), channel.toByte(), number.toByte(),
            0, 0, 0
        )
        return guide.guide2(Guide2Code.GUI_05, data)
    }

    @SuppressLint("SimpleDateFormat")
    fun setTime(): Boolean {
        val format = SimpleDateFormat("yyyyMMddHHmmss").format(Date(System.currentTimeMillis()))
        val guide = Run.getGuide() ?: return false
        val timeByteArray = format.toInt().toByteArray(7)
        val data = byteArrayOf(
            0,
            0,
            timeByteArray[0].toString(16).toByte(),
            timeByteArray[1].toString(16).toByte(),
            timeByteArray[2].toString(16).toByte(),
            timeByteArray[3].toString(16).toByte(),
            timeByteArray[4].toString(16).toByte(),
            timeByteArray[5].toString(16).toByte(),
            timeByteArray[6].toString(16).toByte(),
            0
        )
        return guide.guide2(Guide2Code.GUI_02, data)
    }

    fun setSystemState(
        lightSenior: Boolean,
        notDetectGoods: Boolean,
        lightTemperatureInfo: LightTemperatureInfo
    ): Boolean {
        val guide = Run.getGuide() ?: return false
        val sellingConfig = notDetectGoods.insertByte(lightSenior.insertByte(0, 3), 5)
        val light = when (lightTemperatureInfo.lightStatus) {
            LightStatusEnum.Auto -> 0x00
            LightStatusEnum.AlwaysClose -> 0x01
            LightStatusEnum.AlwaysOpen -> 0x02
        }
        val lift = when (lightTemperatureInfo.leftRoomStatus) {
            TemperatureStateEnum.Cool -> 0x00
            TemperatureStateEnum.Hot -> 0x01
            TemperatureStateEnum.Disable -> 0x02
        }
        val right = when (lightTemperatureInfo.rightRoomStatus) {
            TemperatureStateEnum.Cool -> 0x00
            TemperatureStateEnum.Hot -> 0x01
            TemperatureStateEnum.Disable -> 0x02
        }
        val data = byteArrayOf(
            0, 0,
            lightTemperatureInfo.deviceNumber.toByte(),
            sellingConfig,
            light.toByte(),
            lightTemperatureInfo.openLightTime.startHour.toBCDByte(),
            lightTemperatureInfo.openLightTime.startMin.toBCDByte(),
            lightTemperatureInfo.openLightTime.endHour.toBCDByte(),
            lightTemperatureInfo.openLightTime.endMin.toBCDByte(),
            lift.toByte(),
            right.toByte(),
            lightTemperatureInfo.energySavingTime.startHour.toBCDByte(),
            lightTemperatureInfo.energySavingTime.startMin.toBCDByte(),
            lightTemperatureInfo.energySavingTime.endHour.toBCDByte(),
            lightTemperatureInfo.energySavingTime.endMin.toBCDByte(),
            lightTemperatureInfo.settingCoolTemperature.toBCDByte(),
            lightTemperatureInfo.settingHotTemperature.toBCDByte(),
            0
        )
        return guide.guide2(Guide2Code.GUI_06, data)
    }
}